package com.chinawlx.wlxteacher.ui.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher._enum.WLXCalendarMode;
import com.chinawlx.wlxteacher.network.WLXHttpRxHelper;
import com.chinawlx.wlxteacher.network.WLXRxBus;
import com.chinawlx.wlxteacher.network.bean.WLXCalendarBean;
import com.chinawlx.wlxteacher.network.bean.WLXScheduleChangeBean;
import com.chinawlx.wlxteacher.ui.activity.WLXClazzRoomDetailActivity;
import com.chinawlx.wlxteacher.ui.adapter.calendar.WLXCalendarPagerAdapter;
import com.chinawlx.wlxteacher.ui.adapter.calendar.WLXClassListAdapter;
import com.chinawlx.wlxteacher.ui.adapter.interf.OnItemClickListener;
import com.chinawlx.wlxteacher.ui.event.ShowClassEvent;
import com.chinawlx.wlxteacher.ui.fragment.calendar.WLXMonthFragment;
import com.chinawlx.wlxteacher.ui.fragment.calendar.WLXWeekFragment;
import com.chinawlx.wlxteacher.utils.WLXCalendarUtil;
import com.chinawlx.wlxteacher.utils.WLXConstant;
import com.chinawlx.wlxteacher.utils.WLXDateUtil;
import com.chinawlx.wlxteacher.utils.WLXGreenDaoUtil;
import com.chinawlx.wlxteacher.utils.WLXLogUtil;
import com.chinawlx.wlxteacher.utils.WLXSaveSQLiteUtil;
import com.chinawlx.wlxteacher.utils.WLXTeacherConstant;
import com.chinawlx.wlxteacher.widget.WLXCustomToast;
import com.chinawlx.wlxteacher.widget.calendar.WrapContentHeightViewPager;
import com.chinawlx.wlxteacher.wlx_schedule;
import com.chinawlx.wlxteacher.wlx_scheduleDao;
import com.chinawlx.wlxteacher.wlx_schedule_change;
import com.chinawlx.wlxteacher.wlx_schedule_changeDao;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.refreshlayout.BGAMeiTuanRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import rx.Subscriber;
import rx.functions.Action1;

/**
 * Created by wlx on 16/8/9.
 */
public class WLXTimeTableFragment extends WLXBaseFragment implements BGARefreshLayout
        .BGARefreshLayoutDelegate {
    @BindView(R.id.viewPager_calendarMonth)
    WrapContentHeightViewPager mViewPagerMonth;
    @BindView(R.id.viewPager_calendarWeek)
    WrapContentHeightViewPager mViewPagerWeek;
    @BindView(R.id.rv_classList)
    RecyclerView mRvClassList;
    @BindView(R.id.ll_todayNoClass)
    LinearLayout mLlTodayNoClass;
    @BindView(R.id.tv_week)
    TextView mTvWeek;
    @BindView(R.id.tv_month)
    TextView mTvMonth;
    @BindView(R.id.tv_today)
    TextView mTvToday;
    @BindView(R.id.rl_refresh)
    BGARefreshLayout mRlRefresh;


    /**
     * 日历的模式(一进来,默认是月模式)
     */
    private WLXCalendarMode mCalendarMode = WLXCalendarMode.WEEK;

    private ArrayList<wlx_schedule> mSchedules = new ArrayList<>();

    private WLXClassListAdapter mClassListAdapter;
    private final Calendar mCalendar = Calendar.getInstance();
    private Calendar mLastCalendar = WLXCalendarUtil.getLastMonth();
    private Calendar mNextCalendar = WLXCalendarUtil.getNextMonth();
    /**
     * 三个月的第一天和最后一天(用于去第一次请求三个月数据)
     */
    private String mStartDate = "";
    private String mEndDate = "";
    private ArrayList<Fragment> mMonthFragments = new ArrayList<>();
    private ArrayList<Fragment> mWeekFragments = new ArrayList<>();

    private WLXCalendarPagerAdapter mMonthAdapter;
    private WLXCalendarPagerAdapter mWeekAdapter;
    private FragmentManager mFragmentManager;
    private WLXLoadingFragment loadingFragment;
    private Gson gson;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mFragmentManager = getActivity().getSupportFragmentManager();
        loadingFragment = WLXLoadingFragment.getInstance();
        loadingFragment.show(mFragmentManager, "");
//        init();
    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
//            savedInstanceState) {
//        View view = inflater.inflate(R.layout.fragment_time_table, null);
//        ButterKnife.bind(this, view);
//        //初始化下拉刷新
//        initRefreshLayout();
//        gson = new Gson();
//
//        showClassEvent();
//        computeStartAndEndDate();
//        initSchedule();
//
//        return view;
//    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_time_table, null);
        ButterKnife.bind(this, view);
        init();
        return view;
    }
    private void init() {
        gson = new Gson();
        //初始化下拉刷新
        initRefreshLayout();
        gson = new Gson();
        computeStartAndEndDate();
        initSchedule();
        showClassEvent();

    }

    /**
     * 联网请求获取课程表数据
     */
    private void initSchedule() {
        // TODO: 16/10/15 注释下行,方便拿课程数据
        WLXHttpRxHelper.getInstance().schedule(mStartDate, mEndDate, new
//        WLXHttpRxHelper.getInstance().schedule("2016-10-17", "2016-10-20", new
                Subscriber<WLXCalendarBean>() {

                    @Override
                    public void onCompleted() {
                        loadingFragment.dismiss();
                        //==================根据Grade_id去查课程表信息,然后按天分组===begin
                        WLXConstant.mSchedulehashMap.clear();//因为保存的整个app全局的,所以下次进入app时,都要先清除,
                        // 再查询插入数据
                        final List<wlx_schedule> scheduleList = WLXGreenDaoUtil.getScheduleDao()
                                .queryBuilder().where(wlx_scheduleDao.Properties.Teacher_user_id.eq
                                        (WLXConstant.USER_ID)).orderAsc(wlx_scheduleDao.Properties
                                        .Schedule_date).list();
                        long key;//日期
                        for (wlx_schedule schedule : scheduleList) {
                            //=============按天分组
                            if (schedule.getSchedule_date() % (60 * 60 * 24) == 0) {
                                key = schedule.getSchedule_date() / (60 * 60 * 24);
                            } else {
                                key = schedule.getSchedule_date() / (60 * 60 * 24) + 1;
                            }
                            if (!WLXConstant.mSchedulehashMap.containsKey(key)) {
                                WLXConstant.mSchedulehashMap.put(key, new ArrayList<wlx_schedule>
                                        ());
                            }
                            WLXConstant.mSchedulehashMap.get(key).add(schedule);
                        }
                        //==================根据Grade_id去查课程表信息,然后按天分组=====end


                        //在fragment读数据库展示界面
                        initCalendar();
                        mRlRefresh.endRefreshing();
                    }

                    @Override
                    public void onError(Throwable e) {
                        onCompleted();
                    }

                    @Override
                    public void onNext(WLXCalendarBean calendarBean) {
                        if (calendarBean.getCode() == 0) {
                            if (calendarBean.getData() != null) {
                                final List<WLXCalendarBean.DataBean> scheduleList = calendarBean
                                        .getData();
                                if (scheduleList != null && scheduleList.size() > 0) {
                                    //========存储数据到wlx_schedule表
                                    WLXSaveSQLiteUtil.saveWlxSchedule(scheduleList);
                                }
                            }
                        } else {
                            WLXCustomToast.show(calendarBean.getMsg());
                        }
                    }
                });
    }




    /**
     * 展示课程便事件接收
     */
    private void showClassEvent() {
        WLXRxBus.getInstance().toObserverable().subscribe(new Action1<Object>() {
            @Override
            public void call(Object event) {
                if (event instanceof ShowClassEvent) {
                    showClassList();
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {

            }
        });
    }

    /**
     * 展示课程列表
     */
    private void showClassList() {
        mSchedules.clear();
        final ArrayList<wlx_schedule> list = WLXConstant.mSchedulehashMap.get(WLXConstant.date);
        if (list != null) {
            mSchedules.addAll(list);

        }
        if (mSchedules != null && mSchedules.size() > 0) {
            mLlTodayNoClass.setVisibility(View.GONE);
            mRvClassList.setVisibility(View.VISIBLE);
            if (mClassListAdapter == null) {
                mRvClassList.setLayoutManager(new LinearLayoutManager(mContext));
                mClassListAdapter = new WLXClassListAdapter(mContext, mSchedules);
                mRvClassList.setAdapter(mClassListAdapter);
                mClassListAdapter.setOnItemClickListener(new OnItemClickListener() {


                    @Override
                    public void onItemClick(View view, int position) {
                        ImageView stateImage = (ImageView) view.findViewById(R.id.iv_state);
                        //课程管理状态view可见时,点击不可跳转,并为其设置长按弹窗事件
                        WLXLogUtil.printE("stateImageId: " + stateImage.getId());
                        ;
                        if (stateImage.isShown()) {
                            if (stateImage.getDrawable().getConstantState().equals(getResources()
                                    .getDrawable(R.drawable.tiao).getConstantState())) {
                                WLXCustomToast.show("长按显示调课信息");

                            } else if (stateImage.getDrawable().getConstantState().equals
                                    (getResources()
                                            .getDrawable(R.drawable.qing).getConstantState())) {
                                WLXCustomToast.show("长按显示请假信息");

                            } else if (stateImage.getDrawable().getConstantState().equals
                                    (getResources()
                                            .getDrawable(R.drawable.jia).getConstantState())) {
                                WLXCustomToast.show("长按显示加课信息");

                            } else if (stateImage.getDrawable().getConstantState().equals
                                    (getResources()
                                            .getDrawable(R.drawable.ting).getConstantState())) {
                                WLXCustomToast.show("长按显示停课信息");

                            }
                        } else {
                            WLXLogUtil.printE("onItemClick: " + position);
                            String schedule_id = mSchedules.get(position).getSchedule_id();
                            String grade_id = Integer.toString(mSchedules.get(position)
                                    .getGrade_id());
                            WLXLogUtil.printE("schedule_id: " + schedule_id);
                            WLXLogUtil.printE("grade_id: " + grade_id);
                            Bundle bundle = new Bundle();
                            bundle.putString(WLXTeacherConstant.SCHEDULE_ID, schedule_id);
                            bundle.putString(WLXTeacherConstant.GRADE_ID, grade_id);
                            startActivity(WLXClazzRoomDetailActivity.class, false, bundle);
                        }

                    }

                    @Override
                    public void onItemLongClick(View view, int position) {
                        ImageView stateImage = (ImageView) view.findViewById(R.id.iv_state);
                        if (stateImage.isShown()) {
//                            WLXCustomToast.show("长按信息");
                            WLXCalendarBean.DataBean.ExtendBean extendBean = gson.fromJson
                                    (mSchedules.get(position).getExtend_info(), WLXCalendarBean
                                            .DataBean.ExtendBean.class);
                            String teacherChangeId = Integer.toString(extendBean
                                    .getTeacher_change_id());
//                            WLXScheduleStateFragment stateFragment = WLXScheduleStateFragment
//                                    .getInstance(teacherChangeId);
                            loadingFragment.show(mFragmentManager, "");
                            requestNetForScheduleChange(teacherChangeId);

                        }
                    }
                });
            } else {
                //切换了不同的日期,更新课程列表
                mClassListAdapter.notifyDataSetChanged();
            }
        } else {
            mRvClassList.setVisibility(View.GONE);
            mLlTodayNoClass.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 计算三个月的第一天和最后一天的日期
     */
    private void computeStartAndEndDate() {
        //=====================计算三个月的第一天的日期begin
        mLastCalendar.set(mLastCalendar.get(Calendar.YEAR), mLastCalendar.get(Calendar.MONTH), 1);
        int pos = mLastCalendar.get(Calendar.DAY_OF_WEEK) - 1;
        mLastCalendar.add(Calendar.DAY_OF_MONTH, -pos);
        mStartDate = mLastCalendar.get(Calendar.YEAR) + "-" + (mLastCalendar.get(Calendar.MONTH)
                + 1) + "-" + mLastCalendar.get(Calendar.DAY_OF_MONTH);
        mLastCalendar = WLXCalendarUtil.getLastMonth();//还原日期
        //=====================计算三个月的第一天的日期end


        //=====================计算三个月的最后一天的日期begin
        mNextCalendar.set(mNextCalendar.get(Calendar.YEAR), mNextCalendar.get(Calendar.MONTH), 1);
        pos = 42 - mNextCalendar.get(Calendar.DAY_OF_WEEK);
        mNextCalendar.add(Calendar.DAY_OF_MONTH, pos);
        mEndDate = mNextCalendar.get(Calendar.YEAR) + "-" + (mNextCalendar.get(Calendar.MONTH) +
                1) + "-" + mNextCalendar.get(Calendar.DAY_OF_MONTH);
        mNextCalendar = WLXCalendarUtil.getNextMonth();//还原日期
        //=====================计算三个月的最后一天的日期end
    }

    private void initCalendar() {
        switch (mCalendarMode) {
            case MONTH:
                initMonthFragment();
                break;
            case WEEK:
                initWeekFragment();
                break;
        }
        initViewPager();
    }

    /**
     * 先初始化周模式下Fragment待ViewPager使用
     */
    private void initWeekFragment() {
        if (mWeekFragments == null || mWeekFragments.size() == 0) {
            //第一个月
            int weekNumOfLastMonth = WLXCalendarUtil.getWeekNumOfLastMonth();
            for (int i = 1; i <= weekNumOfLastMonth; i++) {
                WLXWeekFragment weekFragment = WLXWeekFragment.newInstance(mLastCalendar.get
                        (Calendar.YEAR), mLastCalendar.get(Calendar.MONTH) + 1, i);
                mWeekFragments.add(weekFragment);
            }

            //第二个月(当前月份)
            int weekNum = mCalendar.getActualMaximum(Calendar.WEEK_OF_MONTH);
            for (int i = 1; i <= weekNum; i++) {
                WLXWeekFragment weekFragment = WLXWeekFragment.newInstance(mCalendar.get(Calendar
                        .YEAR), mCalendar.get(Calendar.MONTH) + 1, i);
                mWeekFragments.add(weekFragment);
            }

            //第三个月
            int weekNumOfNextMonth = WLXCalendarUtil.getWeekNumOfNextMonth();
            for (int i = 1; i <= weekNumOfNextMonth; i++) {
                WLXWeekFragment weekFragment = WLXWeekFragment.newInstance(mNextCalendar.get
                        (Calendar.YEAR), mNextCalendar.get(Calendar.MONTH) + 1, i);
                mWeekFragments.add(weekFragment);
            }

        }
    }

    /**
     * 先初始化月份模式下Fragment,待ViewPager使用
     */
    private void initMonthFragment() {
        if (mMonthFragments == null || mMonthFragments.size() == 0) {
            //第一个月的Fragment
            WLXMonthFragment monthFragment1 = WLXMonthFragment.newInstance(mLastCalendar.get
                    (Calendar.YEAR), mLastCalendar.get(Calendar.MONTH) + 1);
            mMonthFragments.add(monthFragment1);

            //第二个月的Fragment(当前月份)
            WLXMonthFragment monthFragment2 = WLXMonthFragment.newInstance(mCalendar.get(Calendar
                    .YEAR), mCalendar.get(Calendar.MONTH) + 1);
            mMonthFragments.add(monthFragment2);

            //第三个月的Fragment
            WLXMonthFragment monthFragment3 = WLXMonthFragment.newInstance(mNextCalendar.get
                    (Calendar.YEAR), mNextCalendar.get(Calendar.MONTH) + 1);
            mMonthFragments.add(monthFragment3);
        }
    }



    private void initRefreshLayout() {
        mRlRefresh.setDelegate(this);
        BGAMeiTuanRefreshViewHolder meiTuanRefreshViewHolder = new BGAMeiTuanRefreshViewHolder
                (this.getContext(), false);
        meiTuanRefreshViewHolder.setPullDownImageResource(R.drawable.yuanq);
        meiTuanRefreshViewHolder.setChangeToReleaseRefreshAnimResId(R.drawable
                .bga_refresh_mt_change_to_release_refresh);
        meiTuanRefreshViewHolder.setRefreshingAnimResId(R.drawable.bga_refresh_mt_refreshing);

        mRlRefresh.setRefreshViewHolder(meiTuanRefreshViewHolder);
    }



    @OnClick({R.id.tv_week, R.id.tv_month, R.id.tv_today})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_week:
                //noinspection deprecation
                mTvMonth.setBackgroundDrawable(null);
                mTvWeek.setBackgroundResource(R.drawable.shape_yellow_circle);
                mCalendarMode = WLXCalendarMode.WEEK;
                mViewPagerMonth.setVisibility(View.GONE);
                mViewPagerWeek.setVisibility(View.VISIBLE);
                initCalendar();
                break;
            case R.id.tv_month:
                //noinspection deprecation
                mTvWeek.setBackgroundDrawable(null);
                mTvMonth.setBackgroundResource(R.drawable.shape_yellow_circle);
                mCalendarMode = WLXCalendarMode.MONTH;
                mViewPagerWeek.setVisibility(View.GONE);
                mViewPagerMonth.setVisibility(View.VISIBLE);
                initCalendar();
                break;
            case R.id.tv_today:
                initViewPager();
                break;
        }
    }

    private void initViewPager() {
        switch (mCalendarMode) {
            case WEEK:
                if (mWeekAdapter == null) {
                    mWeekAdapter = new WLXCalendarPagerAdapter(mFragmentManager, mWeekFragments);
                    mViewPagerWeek.setAdapter(mWeekAdapter);
                    mViewPagerWeek.setOffscreenPageLimit(16);
                }else {
                    mWeekAdapter.update();
                }

                if (!(mRlRefresh.getCurrentRefreshStatus() == BGARefreshLayout.RefreshStatus
                        .REFRESHING)) {
                    mViewPagerWeek.setCurrentItem(WLXCalendarUtil.getWeekNumOfLastMonth() +
                            mCalendar.get(Calendar.WEEK_OF_MONTH) - 1);
                }

                //========初始化后,单元格背景初始化,下拉刷新时点击的单元格不初始化======
                if (!(mRlRefresh.getCurrentRefreshStatus() == BGARefreshLayout.RefreshStatus
                        .REFRESHING) && WLXWeekFragment.mClickTvDay != null) {
                    //noinspection deprecation
                    WLXWeekFragment.mClickTvDay.setBackgroundDrawable(null);
                }
                if (!(mRlRefresh.getCurrentRefreshStatus() == BGARefreshLayout.RefreshStatus
                        .REFRESHING) && WLXWeekFragment.mTodayView != null) {
                    if (WLXWeekFragment.mClickTvDay != null) {
                        //初始化之后,将上一次点击的单元格初始化为今天
                        WLXWeekFragment.mClickTvDay = WLXWeekFragment.mTodayView;
                    }
                    WLXWeekFragment.mTodayView.setBackgroundResource(R.drawable
                            .shape_yellow_circle);
                }
                break;
            case MONTH:
                if (mMonthAdapter == null) {
                    mMonthAdapter = new WLXCalendarPagerAdapter(mFragmentManager, mMonthFragments);
                    mViewPagerMonth.setAdapter(mMonthAdapter);
                    mViewPagerMonth.setOffscreenPageLimit(2);//至少得缓存2个
                } else {
                    mMonthAdapter.update();
                }
                if (!(mRlRefresh.getCurrentRefreshStatus() == BGARefreshLayout.RefreshStatus
                        .REFRESHING)) {
                    mViewPagerMonth.setCurrentItem(1);
                }

                //========初始化后,单元格背景初始化,下拉刷新时点击的单元格不初始化======
                if (!(mRlRefresh.getCurrentRefreshStatus() == BGARefreshLayout.RefreshStatus
                        .REFRESHING) && WLXMonthFragment.mClickTvDay != null) {
                    //noinspection deprecation
                    WLXMonthFragment.mClickTvDay.setBackgroundDrawable(null);
                }
                if (!(mRlRefresh.getCurrentRefreshStatus() == BGARefreshLayout.RefreshStatus
                        .REFRESHING) && WLXMonthFragment.mTodayView != null) {
                    if (WLXMonthFragment.mClickTvDay != null) {
                        //初始化之后,将上一次点击的单元格初始化为今天
                        WLXMonthFragment.mClickTvDay = WLXMonthFragment.mTodayView;
                    }
                    WLXMonthFragment.mTodayView.setBackgroundResource(R.drawable
                            .shape_yellow_circle);
                }
                break;
        }
        //一进来显示当天的课程信息(切换月和周模式也是加载当天),下拉刷新时不更新选中的日期
        if (!(mRlRefresh.getCurrentRefreshStatus() == BGARefreshLayout.RefreshStatus.REFRESHING)) {
            WLXConstant.date = WLXConstant.TODAY_CALENDAR.getTimeInMillis() / (60 * 60 * 24 * 1000);
        }
        showClassList();
    }

    /**
     * 请求课程改变的数据
     *
     * @param teacherChangeId
     */
    private void requestNetForScheduleChange(final String teacherChangeId) {
        WLXHttpRxHelper.getInstance().getScheduleChange(teacherChangeId, new
                Subscriber<WLXScheduleChangeBean>() {


                    @Override
                    public void onCompleted() {
                        loadingFragment.dismiss();
                        List<wlx_schedule_change> scheduleChanges = WLXGreenDaoUtil
                                .getScheduleChangeDao().queryBuilder()
                                .where
                                        (wlx_schedule_changeDao.Properties.User_id
                                                .eq(WLXConstant.USER_ID))
                                .where
                                        (wlx_schedule_changeDao.Properties.Schedule_change_id
                                                .eq(teacherChangeId)).list();
                        String grade_title = WLXGreenDaoUtil.getScheduleDao().queryBuilder()
                                .where(wlx_scheduleDao.Properties.Grade_id.eq(scheduleChanges.get(0)
                                        .getGrade_id())).list().get(0).getGrade_title();
                        String reason_code = scheduleChanges.get(0).getReason_code();
                        String type_code = scheduleChanges.get(0).getType_code();
                        Long start_date = scheduleChanges.get(0).getStart_date();
                        Long end_date = scheduleChanges.get(0).getEnd_date();
                        Long new_start_date = scheduleChanges.get(0).getNew_start_date();

                        WLXScheduleStateFragment stateFragment = WLXScheduleStateFragment
                                .getInstance();
                        stateFragment.setGradeTitle(grade_title);
                        stateFragment.setTypeKey(type_code);
                        stateFragment.setReasonKey(reason_code);
                        stateFragment.setStartDate(start_date);
                        stateFragment.setEndDate(end_date);
                        stateFragment.setNewStartDate(new_start_date);
                        stateFragment.show(mFragmentManager, "");
                    }

                    @Override
                    public void onError(Throwable e) {
                        onCompleted();
                    }

                    @Override
                    public void onNext(WLXScheduleChangeBean changeBean) {
                        if (changeBean.getCode() == 0) {
                            WLXScheduleChangeBean.DataBean dataBean = changeBean.getData();
                            String schedule_change_id = Integer.toString(dataBean.getId());
                            int user_id = dataBean.getUser_id();
                            int grade_id = dataBean.getGrade_id();
                            String schedule_ids = dataBean.getSchedule_ids();
                            int is_teacher = dataBean.isIs_teacher() == true ? 1 : 0;
                            String reason_code = dataBean.getReason().getKey();
                            String type_code = dataBean.getType().getKey();
                            String remark = dataBean.getRemark();
                            String extend_info = gson.toJson(dataBean.getExtend());
                            long start_date = WLXDateUtil.getStringToDate(dataBean.getStart_date());
                            long end_date = WLXDateUtil.getStringToDate(dataBean.getEnd_date());
                            long new_start_date = WLXDateUtil.getStringToDate(dataBean
                                    .getNew_start_date());
                            long new_end_date = WLXDateUtil.getStringToDate(dataBean
                                    .getNew_end_date());
                            long last_modified_date = WLXDateUtil.getStringToDate(dataBean
                                    .getLast_modified_date());

                            wlx_schedule_change wlxSchuduleChange = new wlx_schedule_change
                                    (schedule_change_id, user_id, grade_id, schedule_ids,
                                            is_teacher, reason_code, type_code, remark,
                                            extend_info, start_date, end_date, new_start_date,
                                            new_end_date, last_modified_date);
                            List<wlx_schedule_change> wlxScheduleChanges = WLXGreenDaoUtil
                                    .getScheduleChangeDao
                                            ().queryBuilder().where(wlx_schedule_changeDao
                                            .Properties
                                            .Schedule_change_id.eq(schedule_change_id)).list();
                            if (wlxScheduleChanges.size() > 0) {
                                if (last_modified_date > wlxScheduleChanges.get(0)
                                        .getLast_modified_date()) {
                                    WLXGreenDaoUtil.getScheduleChangeDao().update
                                            (wlxSchuduleChange);
                                }
                            } else {
                                WLXGreenDaoUtil.getScheduleChangeDao().insert(wlxSchuduleChange);
                            }

                        }
                    }
                });
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        //重新请求数据
        initSchedule();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }
}
